package com.joker.demo.admin.controller;

import com.joker.demo.admin.common.Result;
import com.joker.demo.admin.domain.dto.JobInfoQueryDTO;
import com.joker.demo.admin.domain.dto.JobLogQueryDTO;
import com.joker.demo.admin.domain.dto.JobDailyExecutionQueryDTO;
import com.joker.demo.admin.domain.vo.JobInfoVO;
import com.joker.demo.admin.domain.vo.JobLogVO;
import com.joker.demo.admin.domain.vo.JobDailyExecutionVO;
import com.joker.demo.admin.domain.vo.PageResult;
import com.joker.demo.admin.service.JobInfoService;
import com.joker.demo.admin.service.JobLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 任务信息和日志控制器
 *
 * @author : feixiang.li
 * @since : 2025-07-26 19:38
 */
@Slf4j
@RestController
@RequestMapping("/api/job")
@CrossOrigin
public class JobLogInfoController {

    @Resource
    private JobInfoService jobInfoService;

    @Resource
    private JobLogService jobLogService;

    /**
     * 分页查询任务信息列表
     *
     * @param jobInfoQueryDTO 查询参数
     * @return 任务信息分页结果
     */
    @PostMapping("/info/page")
    public Result<PageResult<JobInfoVO>> queryJobInfoPage(@RequestBody JobInfoQueryDTO jobInfoQueryDTO) {
        try {
            PageResult<JobInfoVO> result = jobInfoService.queryJobInfoPage(jobInfoQueryDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询任务信息分页失败", e);
            return Result.error("查询任务信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取任务详情
     *
     * @param jobId 任务ID
     * @return 任务详情
     */
    @GetMapping("/info/detail/{jobId}")
    public Result<JobInfoVO> getJobInfoDetail(@PathVariable("jobId") String jobId) {
        try {
            JobInfoVO jobInfoVO = jobInfoService.getJobInfoDetail(Integer.parseInt(jobId));
            if (jobInfoVO == null) {
                return Result.error("任务不存在");
            }
            return Result.success(jobInfoVO);
        } catch (Exception e) {
            log.error("获取任务详情失败，jobId: {}", jobId, e);
            return Result.error("获取任务详情失败：" + e.getMessage());
        }
    }

    /**
     * 手动触发任务
     *
     * @param jobId 任务ID
     * @return 操作结果
     */
    @PostMapping("/info/trigger/{jobId}")
    public Result<String> triggerJob(@PathVariable("jobId") String jobId) {
        try {
            log.info("手动触发任务，jobId: {}", jobId);
            boolean success = jobInfoService.triggerJob(Integer.parseInt(jobId));
            if (success) {
                return Result.success("任务触发成功");
            } else {
                return Result.error("任务触发失败");
            }
        } catch (Exception e) {
            log.error("手动触发任务失败，jobId: {}", jobId, e);
            return Result.error("任务触发失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询任务日志
     *
     * @param jobLogQueryDTO 查询参数
     * @return 任务日志分页结果
     */
    @PostMapping("/log/page")
    public Result<PageResult<JobLogVO>> queryJobLogPage(@RequestBody JobLogQueryDTO jobLogQueryDTO) {
        try {
            PageResult<JobLogVO> result = jobLogService.queryJobLogPage(jobLogQueryDTO);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询任务日志分页失败", e);
            return Result.error("查询任务日志失败：" + e.getMessage());
        }
    }

    /**
     * 获取任务日志详情
     *
     * @param jobId 任务ID
     * @return 任务日志详情
     */
    @GetMapping("/log/detail/{jobId}")
    public Result<JobLogVO> getJobLogDetail(@PathVariable("jobId") String jobId) {
        try {
            JobLogVO jobLogVO = jobLogService.getJobLogDetail(Integer.parseInt(jobId));
            if (jobLogVO == null) {
                return Result.error("任务日志不存在");
            }
            return Result.success(jobLogVO);
        } catch (Exception e) {
            log.error("获取任务日志详情失败，jobId: {}", jobId, e);
            return Result.error("获取任务日志详情失败：" + e.getMessage());
        }
    }

    /**
     * 手动终止任务
     *
     * @param jobId 任务ID
     * @return 操作结果
     */
    @PostMapping("/log/stop/{jobId}")
    public Result<String> stopJob(@PathVariable("jobId") String jobId) {
        try {
            boolean success = jobLogService.stopJob(Integer.parseInt(jobId));
            if (success) {
                return Result.success("任务终止成功");
            } else {
                return Result.error("任务终止失败");
            }
        } catch (Exception e) {
            log.error("手动终止任务失败，jobId: {}", jobId, e);
            return Result.error("任务终止失败：" + e.getMessage());
        }
    }

    /**
     * 查询一天内任务执行情况
     *
     * @param queryDTO 查询参数
     * @return 任务执行情况
     */
    @PostMapping("/daily-execution")
    public Result<JobDailyExecutionVO> queryJobDailyExecution(@RequestBody JobDailyExecutionQueryDTO queryDTO) {
        try {
            JobDailyExecutionVO result = jobLogService.queryJobDailyExecution(queryDTO);
            if (result == null) {
                return Result.error("查询失败或数据不存在");
            }
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询任务一天内执行情况失败", e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}
